package com.example.demo1.Controller;

import com.example.demo1.Bean.UserBean;
import com.example.demo1.Bean.UserStates;
import com.example.demo1.Service.UserService;
import com.example.demo1.Service.UserstatusService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageInfo;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Controller
@RequestMapping("/admin")
public class UserContro {
    @Resource
    UserService userservice;
    @Resource
    UserstatusService userstatusService;

    List<UserStates> userstatusList = new ArrayList<>();
    List<UserStates> userstatusList1 = new ArrayList<>();
    List<UserBean> userList = new ArrayList<>();
    PageInfo pageInfo = new PageInfo();
    int pageSize = 10;

    String userdate = null;
    String userid = null;
    LocalDate userdate1 = null;
    String kw ="";

    @RequestMapping("/show")
    String showAllUserstatus(Model model,String pageNum,String state) throws JsonProcessingException {
        int count1 = 0;
        LocalDate nyr = LocalDate.now();
        System.out.println(state);
        if (pageNum==null|| Objects.equals(pageNum, "0"))
            pageNum="1";
        if(state==null)
            state= String.valueOf(0);

        pageInfo = userstatusService.getAllUserStatus(Integer.parseInt(pageNum), pageSize);
        if(state.equals("1")){
            kw=null;
        }
        if(kw!=null)
            pageInfo = userstatusService.getAllUserStatusBykw(Integer.parseInt(pageNum), pageSize, kw);


        userList = userservice.getAllUser();
        System.out.println(userList);
        for(int i = 0;i<userList.size();i++){
            userid = String.valueOf(userList.get(i).getUserId());
            userstatusList1 = userstatusService.SearchUserStatusByid(userid);
            System.out.println("---------------");
            System.out.println(userstatusList1);
            System.out.println("--------");
            for(int x = 0;x<userstatusList1.size();x++){
                userdate1 = userstatusList1.get(x).getDate();
                System.out.println(userdate1);
                if(userdate1.isEqual(nyr)) {
                    count1++;
                }
            }
        }
        userstatusList = pageInfo.getList();
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("userstatusList",userstatusList);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("count1", userList.size()-count1);
        model.addAttribute("keyword",kw);

        StringBuffer _temp = new StringBuffer();
        _temp.append("[");

        _temp.append("{\"name\": \"").append("已打卡人数")
                    .append("\",\"value\":").append(count1).append("},");
        _temp.append("{\"name\":\"").append("未打卡人数")
                .append("\",\"value\":").append(userList.size()-count1).append("}");
        _temp.append("]");

        System.out.println(_temp);
        try {
            FileWriter fw=new FileWriter("F:/work/idea-workspace/demo1/src/main/resources/static/assets/data/data.json",false);
            fw.write(_temp.toString());
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "show";
    }

    @RequestMapping("/searchBykw")
    String showUserstatusBykw(Model model,String pageNum,String keyword) throws JsonProcessingException {
        int count1 = 0;
        LocalDate nyr = LocalDate.now();
        kw=keyword;
        if (pageNum==null|| Objects.equals(pageNum, "0"))
            pageNum="1";

        pageInfo = userstatusService.getAllUserStatusBykw(Integer.parseInt(pageNum), pageSize, kw);
        System.out.println("running kw == " + kw);

        userList = userservice.getAllUser();
        for(int i = 0;i<userList.size();i++){
            userdate = String.valueOf(userList.get(i).getLastdate());
            userid = String.valueOf(userList.get(i).getUserId());
            userstatusList1 = userstatusService.SearchUserStatusByid(userid);
            for(int x = 0;x<userstatusList1.size();x++){
                userdate1 = userstatusList1.get(x).getDate();
                if(userdate1.isEqual(nyr)) {
                    count1++;
                    break;
                }
            }
        }
        userstatusList = pageInfo.getList();
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("userstatusList",userstatusList);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("count1", userList.size()-count1);
        model.addAttribute("keyword",kw);

        StringBuffer _temp = new StringBuffer();
        _temp.append("[");

        _temp.append("{\"name\": \"").append("已打卡人数")
                .append("\",\"value\":").append(count1).append("},");
        _temp.append("{\"name\":\"").append("未打卡人数")
                .append("\",\"value\":").append(userList.size()-count1).append("}");
        _temp.append("]");

        System.out.println(_temp);
        try {
            FileWriter fw=new FileWriter("F:/work/idea-workspace/demo1/src/main/resources/static/assets/data/data.json",false);
            fw.write(_temp.toString());
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "show";
    }

    @RequestMapping("/toInsertUser")
    String toInsertGoodVersion(Model model,String userid){
        System.out.println("进入加入");
        model.addAttribute("userid",userid);
        return "insertUser";
    }

    @RequestMapping("/insertUser")
    String insertGoodVersion(Model model,String userid,String username,String usersex,String userphone,String usearddress){
        LocalDate nyr = LocalDate.now();
        userservice.insertUser(userid,username,usersex,userphone,usearddress,nyr);
        System.out.println(userphone);
        return "redirect:/admin/user";
    }

}
